package com.ybb.stock.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ybb.order.service.OrderService;
import com.ybb.stock.entity.GoodsStockChangeEntity;
import com.ybb.stock.entity.GoodsStocksEntity;
import com.ybb.stock.mapper.GoodsMapper;
import com.ybb.stock.mapper.GoodsStockChangeMapper;
import com.ybb.stock.mapper.GoodsStocksMapper;
import com.ybb.stock.vo.StockChangeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class GoodsStockServiceImpl implements GoodsStockService{

    @Autowired
    private GoodsStockChangeMapper goodsStockChangeMapper;
    @Autowired
    private GoodsStocksMapper goodsStocksMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    public Integer getGoodsStockWithLock(String sku, String merchant) {
        Integer num = goodsStocksMapper.getGoodsStockInLock(sku, merchant);
        return num;
    }

    /**
     * 商品入库
     */
    public void putInStockGoods(String merchant, StockChangeVo stockChange) {
        GoodsStockChangeEntity goodsStockChange = new GoodsStockChangeEntity();
        goodsStockChange.setSku(stockChange.getSku());
        goodsStockChange.setNum(stockChange.getNum());
        goodsStockChange.setMerchant(merchant);
        //添加库存条目记录
        goodsStockChangeMapper.insert(goodsStockChange);

        //获取当前库存，并锁住表（其它人不能查、不能改)
        Integer num = goodsStocksMapper.getGoodsStockInLock(stockChange.getSku(), merchant);
        if (num == null) {//如果存在就更新
            GoodsStocksEntity goodsStocks = new GoodsStocksEntity();
            goodsStocks.setSku(stockChange.getSku());
            goodsStocks.setNum(stockChange.getNum());
            goodsStocks.setMerchant(merchant);
            goodsStocksMapper.insertStocks(goodsStocks);
        } else {//否则就添加库存记录
            goodsStocksMapper.incrementStocks(stockChange.getSku(), stockChange.getNum(), merchant);
        }
    }

    /**
     * 商品暂出库
     */
    @Transactional
    public void checkoutStage(String merchant, StockChangeVo stockChange, Integer orderId) {
        //减库存
        goodsStocksMapper.incrementStocks(stockChange.getSku(), 0 - stockChange.getNum(), merchant);
        //暂存起来
        GoodsStockChangeEntity goodsStockChange = new GoodsStockChangeEntity();
        goodsStockChange.setSku(stockChange.getSku());
        goodsStockChange.setNum(stockChange.getNum());
        goodsStockChange.setMerchant(merchant);
        goodsStockChange.setOrderId(orderId);
        goodsStockChange.setStatus(OrderService.ORDER_STATUS_UNPAY);
        goodsStockChangeMapper.insert(goodsStockChange);
    }

    /**
     * 商品确认出库
     */
    @Transactional
    public void checkoutConfirm(Integer orderId) {
        goodsStockChangeMapper.changeStatus(orderId, OrderService.ORDER_STATUS_PAYED);
    }

    /**
     * 订单商品回库
     */
    @Transactional
    public void checkoutRollback(Integer orderId) {
        QueryWrapper<GoodsStockChangeEntity> query = new QueryWrapper<>();
        query.eq("order_id", orderId);
        query.eq("status", OrderService.ORDER_STATUS_UNPAY);
        List<GoodsStockChangeEntity> goods = goodsStockChangeMapper.selectList(query);
        //商品回库
        for (GoodsStockChangeEntity g : goods) {
            goodsStocksMapper.incrementStocks(g.getSku(), g.getNum(), g.getMerchant());
        }
        //出库记录失效
        goodsStockChangeMapper.changeStatus(orderId, OrderService.ORDER_STATUS_CANCEL);

    }

    /**
     * 获取商品价格
     */
    public Double getGoodsPrice(String sku) {
        Date now = Calendar.getInstance().getTime();
        return goodsMapper.getGoodsPrice(sku, now);
    }
}
